| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889 |
- import { NextRequest, NextResponse } from "next/server";
- import { getServerSession } from "next-auth";
- import { authOptions } from "@/lib/auth";
- import { prisma } from "@/lib/prisma";
- // POST /api/appointments/[id]/reject - Rechazar cita (médico)
- export async function POST(
- request: NextRequest,
- { params }: { params: Promise<{ id: string }> }
- ) {
- try {
- const { id } = await params;
- const session = await getServerSession(authOptions);
-
- if (!session?.user?.email) {
- return NextResponse.json({ error: "No autorizado" }, { status: 401 });
- }
- const user = await prisma.user.findUnique({
- where: { email: session.user.email },
- });
- if (!user || user.role !== "DOCTOR") {
- return NextResponse.json(
- { error: "Solo los médicos pueden rechazar citas" },
- { status: 403 }
- );
- }
- const appointment = await prisma.appointment.findUnique({
- where: { id },
- });
- if (!appointment) {
- return NextResponse.json({ error: "Cita no encontrada" }, { status: 404 });
- }
- if (appointment.estado !== "PENDIENTE") {
- return NextResponse.json(
- { error: "Solo se pueden rechazar citas pendientes" },
- { status: 400 }
- );
- }
- const body = await request.json();
- const { motivoRechazo } = body;
- if (!motivoRechazo) {
- return NextResponse.json(
- { error: "Debe proporcionar un motivo de rechazo" },
- { status: 400 }
- );
- }
- const updated = await prisma.appointment.update({
- where: { id },
- data: {
- estado: "RECHAZADA",
- medicoId: user.id,
- motivoRechazo,
- },
- include: {
- paciente: {
- select: {
- id: true,
- name: true,
- lastname: true,
- email: true,
- profileImage: true,
- },
- },
- medico: {
- select: {
- id: true,
- name: true,
- lastname: true,
- email: true,
- profileImage: true,
- },
- },
- },
- });
- return NextResponse.json(updated);
- } catch (error) {
- console.error("Error al rechazar cita:", error);
- return NextResponse.json({ error: "Error al rechazar cita" }, { status: 500 });
- }
- }
|